<?php
/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2020/1/19
 * Time: 16:07
 */

namespace tools;


class verify
{
//    验证码长度
    protected $codeLen;
//    验证码场景
    protected $name;

    public function __construct($option=[])
    {
        $this->codeLen = $option['codeLen'];
        $this->name = $option['name'];
    }

    /**
     * 讲code保存session
     */
    function save_code(){
        $code = $this->generate();
        session('verify_'.$this->name,$code);
        return $code;
    }

    /**
     * 生成
     */
    function generate()
    {
        $str="abcdefghijkmnpqrstuvwxyz0123456789ABCDEFGHIGKLMNPQRSTUVWXYZ";
        $code = '';//验证码
        for ($i = 0, $mt_rand_max = strlen($str) - 1; $i < $this->codeLen; $i++)
        {
            $code .= $str[mt_rand(0, $mt_rand_max)];
        }
        return $code;
    }

    /**
     * 验证
     */
    function chack($code)
    {
        if (session('verify_'.$this->name) == $code){
            session('verify_'.$this->name,null);
            return true;
        }else{
            return false;
        }
    }
}